package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;


    /**
     * 扣减账户余额
     * /api/account/userAccount/checkAndDeduct
     */
    @Override
    @Transactional(rollbackFor = Exception.class) //TODO 分布式事务问题
    public void checkAndDeduct(AccountLockVo accountLockVo) {
        //这里操作user_account和user_account_detail两个表
        int count = userAccountMapper.checkAndDeduct(accountLockVo.getUserId(),accountLockVo.getAmount());
        if(count==0){
            throw new GuiguException(400,"扣减余额失败");
        }
        //添加用户账户明细表
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(accountLockVo.getUserId());
        userAccountDetail.setTradeType(SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);
        userAccountDetail.setTitle(accountLockVo.getContent());
        userAccountDetail.setAmount(accountLockVo.getAmount());
        userAccountDetail.setOrderNo(accountLockVo.getOrderNo());
        userAccountDetailMapper.insert(userAccountDetail);
    }


    /**
     * 获取账户可用余额
     * /api/account/userAccount/getAvailableAmount
     * @return
     */
    @Override
    public BigDecimal getAvailableAmount(Long userId) {
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAccount::getUserId,userId);
        UserAccount userAccount = userAccountMapper.selectOne(lambdaQueryWrapper);
        BigDecimal totalAmount = userAccount.getTotalAmount();
        return totalAmount;
    }



    @Override
    public void saveUserAccount(Long userId) {
        UserAccount userAccount = new UserAccount();
        //初始值
        userAccount.setUserId(userId);
        userAccount.setTotalAmount(new BigDecimal(100));
        userAccount.setAvailableAmount(new BigDecimal(100));
        userAccount.setTotalIncomeAmount(new BigDecimal(100));
        userAccountMapper.insert(userAccount);
        //更新账户记录
        this.saveUserAccountDetail(userId,"初次赠送", SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT,userAccount.getAvailableAmount(),null);

    }


    //更新账户记录
    private void saveUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(userId);
        userAccountDetail.setTitle(title);
        userAccountDetail.setTradeType(tradeType);
        userAccountDetail.setAmount(amount);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetailMapper.insert(userAccountDetail);
    }


    

}
